<?
  session_start();
  if (isset($_GET["order"])) $order = @$_GET["order"];
  if (isset($_GET["type"])) $ordtype = @$_GET["type"];

  if (isset($_POST["filter"])) $filter = @$_POST["filter"];
  if (isset($_POST["filter_field"])) $filterfield = @$_POST["filter_field"];
  $wholeonly = false;
  if (isset($_POST["wholeonly"])) $wholeonly = @$_POST["wholeonly"];

  if (!isset($order) && isset($_SESSION["order"])) $order = $_SESSION["order"];
  if (!isset($ordtype) && isset($_SESSION["type"])) $ordtype = $_SESSION["type"];
  if (!isset($filter) && isset($_SESSION["filter"])) $filter = $_SESSION["filter"];
  if (!isset($filterfield) && isset($_SESSION["filter_field"])) $filterfield = $_SESSION["filter_field"];

?>

<html>
<head>
<title>HISTORICO DE EVENTOS</title>
<meta name="generator" http-equiv="content-type" content="text/html">
<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF;
  }
  a:link { 
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    color: #800080;
    font-family: Arial;
    font-size: 12px;
  }
  .hr {
    background-color: #336699;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:link {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:active {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:visited {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .dr {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .sr {
    background-color: #FFFFCF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
</style>
</head>
<body>
<?php echo "<table class=\"bd\" width=\"100%\"><tr><td class=\"hr\"><h2>Auditoria Orfeo</h2></td></tr></table>" ?>
<?
  $conn = connect();
  $showrecs = 20;
  $pagerange = 10;


  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  $page = @$_GET["page"];
  if (isset($_GET["wholeonly"])) $wholeonly = @$_GET["wholeonly"] == "on";
  if (!isset($page)) $page = 1;

  $sql = @$_POST["sql"];

  switch ($sql) {
    case "insert":
      sql_insert();
      break;
    case "update":
      sql_update();
      break;
    case "delete":
      sql_delete();
      break;
  }

  switch ($a) {
    case "add":
      addrec();
      break;
    case "view":
      viewrec($recid);
      break;
    case "edit":
      editrec($recid);
      break;
    case "del":
      deleterec($recid);
      break;
    default:

      select();
      break;
  }


  if (isset($order)) $_SESSION["order"] = $order;
  if (isset($ordtype)) $_SESSION["type"] = $ordtype;
  if (isset($filter)) $_SESSION["filter"] = $filter;
  if (isset($filterfield)) $_SESSION["filter_field"] = $filterfield;
  if (isset($wholeonly)) $_SESSION["wholeonly"] = $wholeonly;

  ocilogoff($conn);
?>
<?php echo "<table class=\"bd\" width=\"100%\"><tr><td class=\"hr\"></td></tr></table>" ?>
</body>
</html>

<?php function select()
  {

  global $a;
  global $showrecs;
  global $page;
  global $filter;
  global $filterfield;
  global $wholeonly;
  global $order;
  global $ordtype;


  if ($a == "reset") {
    $filter = "";
    $filterfield = "";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
  }

  $checkstr = "";
  if ($wholeonly) $checkstr = " checked";

  if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; }

  $res = sql_select();

  $count = sql_getrecordcount();
//              $count=20;
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {for ($i = 1; $i <= $startrec; ocifetchassoc($res), $i++);}
  $reccount = min($showrecs * $page, $count);
  

?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td><?php echo "Tabla" ?>: Auditoria Orfeo</td></tr>
<tr><td><?php echo "Registros Mostrados " . ($startrec + 1) . " - " . ($reccount) . " De " . ($count) . "" ?></td></tr>
</table>
<hr size="1" noshade>
<form action="historicoeventos.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><b><?php echo "Filtro Personalizado" ?></b>&nbsp;</td>
<td><input type="text" name="filtro" value="<?php echo $filter ?>"></td>
<td><select name="filter_field">
<option value=""><?php echo "Todos" ?></option>
<option value="<?php echo "Codigo" ?>"<?php if ($filterfield == "lp_DEPE_CODI") { echo "selected"; } ?>><?php echo "Codigo" ?></option>
<option value="<?php echo "Fecha Historial" ?>"<?php if ($filterfield == "HIST_FECH") { echo "selected"; } ?>><?php echo "Fecha Historial" ?></option>
<option value="<?php echo "Codigo Usuario" ?>"<?php if ($filterfield == "USUA_CODI") { echo "selected"; } ?>><?php echo "Codigo Usuario" ?></option>
<option value="<?php echo "No Radicacion" ?>"<?php if ($filterfield == "lp_RADI_NUME_RADI") { echo "selected"; } ?>><?php echo "No Radicacion" ?></option>
<option value="<?php echo "Observacion" ?>"<?php if ($filterfield == "HIST_OBSE") { echo "selected"; } ?>><?php echo "Observacion" ?></option>
<option value="<?php echo "Usuario Destino" ?>"<?php if ($filterfield == "USUA_CODI_DEST") { echo "selected"; } ?>><?php echo "Usuario Destino" ?></option>
<option value="<?php echo "Documento Usuario" ?>"<?php if ($filterfield == "USUA_DOC") { echo "selected"; } ?>><?php echo "USUA_DOC" ?></option>
<option value="<?php echo "Anterior Usuario" ?>"<?php if ($filterfield == "USUA_DOC_OLD") { echo "selected"; } ?>><?php echo "USUA_DOC_OLD" ?></option>
<option value="<?php echo "Transaccion" ?>"<?php if ($filterfield == "SGD_TTR_CODIGO") { echo "selected"; } ?>><?php echo "SGD_TTR_CODIGO" ?></option>
<option value="<?php echo "Usuario Autor" ?>"<?php if ($filterfield == "HIST_USUA_AUTOR") { echo "selected"; } ?>><?php echo "HIST_USUA_AUTOR" ?></option>
<option value="<?php echo "Documento Destino" ?>"<?php if ($filterfield == "HIST_DOC_DEST") { echo "selected"; } ?>><?php echo "HIST_DOC_DEST" ?></option>
<option value="<?php echo "DEPE_CODI_DEST" ?>"<?php if ($filterfield == "DEPE_CODI_DEST") { echo "selected"; } ?>><?php echo "DEPE_CODI_DEST" ?></option>
<option value="<?php echo "IP De Usuario" ?>"<?php if ($filterfield == "USUA_IP") { echo "selected"; } ?>><?php echo "USUA_IP" ?></option>
</select></td>
<td><input type="checkbox" name="wholeonly"<?php echo $checkstr ?>><?php echo "Palabras Claves" ?></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="action" value="Aplicar Filtro "></td>
<td><a href="historicoeventos.php?a=reset"><?php echo "Borrar Filtro" ?></a></td>
</tr>
</table>
</form>
<hr size="1" noshade>
<?php showpagenav($page, $pagecount); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<!--
<td class="hr">&nbsp;</td>
<td class="hr">&nbsp;</td>
<td class="hr">&nbsp;</td>
-->
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Cod Dependencia" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Cod Dependencia" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Fecha Historico" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Fecha Historico" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Usuario" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Usuario" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Lp Radicacion" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Lp Radicacion" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Observacion" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Observacion" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Usuario Destino" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Usuario Destino" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Doc Usuario" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Doc Usuario" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Usuario Ant" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Usuario Ant" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Transaccion" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Transaccion" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Autor" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Autor" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Usuario Dest" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Usuario Dest" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Dependencia Destino" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Dependencia Destino" ?></a></td>
<td class="hr"><a class="hr" href="historicoeventos.php?order=<?php echo "Usuario IP" ?>&type=<?php echo $ordtypestr ?>"><?php echo "Usuario IP" ?></a></td>
</tr>
<?php
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = ocifetchassoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<!--
<td class="<?php echo $style ?>"><a href="historicoeventos.php?a=view&recid=<?php echo $i ?>&wholeonly=<?php if ($wholeonly) echo "on"; else echo "off"; ?>"><?php echo "View" ?></a></td>
<td class="<?php echo $style ?>"><a href="historicoeventos.php?a=edit&recid=<?php echo $i ?>&wholeonly=<?php if ($wholeonly) echo "on"; else echo "off"; ?>"><?php echo "Edit" ?></a></td>
<td class="<?php echo $style ?>"><a href="historicoeventos.php?a=del&recid=<?php echo $i ?>&wholeonly=<?php if ($wholeonly) echo "on"; else echo "off"; ?>"><?php echo "Delete" ?></a></td>
    -->
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["lp_DEPE_CODI"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["HIST_FECH"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["USUA_CODI"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["lp_RADI_NUME_RADI"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["HIST_OBSE"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["USUA_CODI_DEST"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["USUA_DOC"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["USUA_DOC_OLD"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["SGD_TTR_CODIGO"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["HIST_USUA_AUTOR"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["HIST_DOC_DEST"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["DEPE_CODI_DEST"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["USUA_IP"]) ?></td>
</tr>
<?php
  }
  ocifreestatement($res);
?>
</table>
<br>
<?php showpagenav($page, $pagecount); ?>
<?php } ?>

<?php function showrow($row, $recid)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo "Dependencia" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["lp_DEPE_CODI"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "HIST_FECH" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["HIST_FECH"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "USUA_CODI" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["USUA_CODI"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "RADI_NUME_RADI" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["lp_RADI_NUME_RADI"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "HIST_OBSE" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["HIST_OBSE"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "USUA_CODI_DEST" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["USUA_CODI_DEST"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "USUA_DOC" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["USUA_DOC"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "USUA_DOC_OLD" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["USUA_DOC_OLD"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "SGD_TTR_CODIGO" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["SGD_TTR_CODIGO"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "HIST_USUA_AUTOR" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["HIST_USUA_AUTOR"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "HIST_DOC_DEST" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["HIST_DOC_DEST"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "DEPE_CODI_DEST" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["DEPE_CODI_DEST"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo "USUA_IP" ?>&nbsp;</td>
<td class="dr"><?php echo htmlspecialchars($row["USUA_IP"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("DEPE_CODI")."&nbsp;" ?></td>
<td class="dr"><select name="DEPE_CODI">
<?php
  $sql = "select DEPE_CODI, DEPE_CODI from ORFEO.DEPENDENCIA";
  $res = ociquery($sql);

  while ($lp_row = ocifetchassoc($res)){
  $val = $lp_row["DEPE_CODI"];
  $caption = $lp_row["DEPE_CODI"];
  if ($row["DEPE_CODI"] == $val) {$selstr = " selected"; } else {$selstr = ""; }
 ?><option value="<?php echo $val ?>"<?php echo $selstr ?>><?php echo $caption ?></option>
<?php } ?></select>
</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Fecha Historial")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="HIST_FECH" value="<?php echo str_replace('"', '&quot;', trim($row["HIST_FECH"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("USUA_CODI")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="USUA_CODI" value="<?php echo str_replace('"', '&quot;', trim($row["USUA_CODI"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("RADI_NUME_RADI")."&nbsp;" ?></td>
<td class="dr"><select name="RADI_NUME_RADI">
<?php
  $sql = "select RADI_NUME_RADI, RADI_NUME_RADI from ORFEO.RADICADO";
  $res = ociquery($sql);

  while ($lp_row = ocifetchassoc($res)){
  $val = $lp_row["RADI_NUME_RADI"];
  $caption = $lp_row["RADI_NUME_RADI"];
  if ($row["RADI_NUME_RADI"] == $val) {$selstr = " selected"; } else {$selstr = ""; }
 ?><option value="<?php echo $val ?>"<?php echo $selstr ?>><?php echo $caption ?></option>
<?php } ?></select>
</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("HIST_OBSE")."&nbsp;" ?></td>
<td class="dr"><textarea cols="35" rows="4" name="HIST_OBSE" maxlength="600"><?php echo str_replace('"', '&quot;', trim($row["HIST_OBSE"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("USUA_CODI_DEST")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="USUA_CODI_DEST" value="<?php echo str_replace('"', '&quot;', trim($row["USUA_CODI_DEST"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("USUA_DOC")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="USUA_DOC" maxlength="14" value="<?php echo str_replace('"', '&quot;', trim($row["USUA_DOC"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("USUA_DOC_OLD")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="USUA_DOC_OLD" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["USUA_DOC_OLD"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("SGD_TTR_CODIGO")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="SGD_TTR_CODIGO" value="<?php echo str_replace('"', '&quot;', trim($row["SGD_TTR_CODIGO"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("HIST_USUA_AUTOR")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="HIST_USUA_AUTOR" maxlength="14" value="<?php echo str_replace('"', '&quot;', trim($row["HIST_USUA_AUTOR"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("HIST_DOC_DEST")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="HIST_DOC_DEST" maxlength="14" value="<?php echo str_replace('"', '&quot;', trim($row["HIST_DOC_DEST"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("DEPE_CODI_DEST")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="DEPE_CODI_DEST" value="<?php echo str_replace('"', '&quot;', trim($row["DEPE_CODI_DEST"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("USUA_IP")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="USUA_IP" maxlength="20" value="<?php echo str_replace('"', '&quot;', trim($row["USUA_IP"])) ?>"></td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<!--<td><a href="historicoeventos.php?a=add"><?php echo "Add Record" ?></a>&nbsp;</td>--!>
<?php if ($page > 1) { ?>
<td><a href="historicoeventos.php?page=<?php echo $page - 1 ?>">&lt;&lt;&nbsp;<?php echo "Prev" ?></a>&nbsp;</td>
<?php } ?>
<?php
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><?php echo $j ?></b></td>
<?php } else { ?>
<td><a href="historicoeventos.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="historicoeventos.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="historicoeventos.php?page=<?php echo $page + 1 ?>"><?php echo "Sig" ?>&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php function showrecnav($a, $recid, $count)
{
  global $wholeonly;
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="historicoeventos.php"><?php echo "Index Page" ?></a></td>
<?php if ($recid > 0) { ?>
<td><a href="historicoeventos.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>&wholeonly=<?php if ($wholeonly) echo "on"; else echo "off"; ?>"><?php echo "Prior Record" ?></a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="historicoeventos.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>&wholeonly=<?php if ($wholeonly) echo "on"; else echo "off"; ?>"><?php echo "Next Record" ?></a></td>
<?php } ?>
</tr>
</table>
<hr size="1" noshade>
<?php } ?>

<?php function addrec()
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="historicoeventos.php"><?php echo "Index Page" ?></a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" action="historicoeventos.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "DEPE_CODI" => "",
  "HIST_FECH" => "",
  "USUA_CODI" => "",
  "RADI_NUME_RADI" => "",
  "HIST_OBSE" => "",
  "USUA_CODI_DEST" => "",
  "USUA_DOC" => "",
  "USUA_DOC_OLD" => "",
  "SGD_TTR_CODIGO" => "",
  "HIST_USUA_AUTOR" => "",
  "HIST_DOC_DEST" => "",
  "DEPE_CODI_DEST" => "",
  "USUA_IP" => "");
showroweditor($row, false);
?>
<p><input type="submit" name="action" value="<?php echo "Post" ?>"></p>
</form>
<?php } ?>

<?php function viewrec($recid)
{
  global $wholeonly;
  $res = sql_select();
  $count = sql_getrecordcount();
  for ($i = 1; $i <= $recid; ocifetchassoc($res), $i++);
  $row = ocifetchassoc($res);
  showrecnav("view", $recid, $count);
?>
<br>
<?php showrow($row, $recid) ?>
<br>
<hr size="1" noshade>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="historicoeventos.php?a=add"><?php echo "Add Record" ?></a></td>
<td><a href="historicoeventos.php?a=edit&recid=<?php echo $recid ?>&wholeonly=<?php if ($wholeonly) echo "on"; else echo "off"; ?>"><?php echo "Edit Record" ?></a></td>
<td><a href="historicoeventos.php?a=del&recid=<?php echo $recid ?>&wholeonly=<?php if ($wholeonly) echo "on"; else echo "off"; ?>"><?php echo "Delete Record" ?></a></td>
</tr>
</table>
<?php
  ocifreestatement($res);
} ?>

<?php function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  for ($i = 1; $i <= $recid; ocifetchassoc($res), $i++);
  $row = ocifetchassoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<form enctype="multipart/form-data" action="historicoeventos.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xDEPE_CODI" value="<?php echo $row["DEPE_CODI"] ?>">
<input type="hidden" name="xHIST_FECH" value="<?php echo $row["HIST_FECH"] ?>">
<input type="hidden" name="xUSUA_CODI" value="<?php echo $row["USUA_CODI"] ?>">
<input type="hidden" name="xRADI_NUME_RADI" value="<?php echo $row["RADI_NUME_RADI"] ?>">
<input type="hidden" name="xUSUA_CODI_DEST" value="<?php echo $row["USUA_CODI_DEST"] ?>">
<input type="hidden" name="xUSUA_DOC" value="<?php echo $row["USUA_DOC"] ?>">
<input type="hidden" name="xUSUA_DOC_OLD" value="<?php echo $row["USUA_DOC_OLD"] ?>">
<input type="hidden" name="xSGD_TTR_CODIGO" value="<?php echo $row["SGD_TTR_CODIGO"] ?>">
<input type="hidden" name="xHIST_USUA_AUTOR" value="<?php echo $row["HIST_USUA_AUTOR"] ?>">
<input type="hidden" name="xHIST_DOC_DEST" value="<?php echo $row["HIST_DOC_DEST"] ?>">
<input type="hidden" name="xDEPE_CODI_DEST" value="<?php echo $row["DEPE_CODI_DEST"] ?>">
<input type="hidden" name="xUSUA_IP" value="<?php echo $row["USUA_IP"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Post"></p>
</form>
<?php
  ocifreestatement($res);
} ?>

<?php function deleterec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  for ($i = 1; $i <= $recid; ocifetchassoc($res), $i++);
  $row = ocifetchassoc($res);
  showrecnav("del", $recid, $count);
?>
<br>
<form action="historicoeventos.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xDEPE_CODI" value="<?php echo $row["DEPE_CODI"] ?>">
<input type="hidden" name="xHIST_FECH" value="<?php echo $row["HIST_FECH"] ?>">
<input type="hidden" name="xUSUA_CODI" value="<?php echo $row["USUA_CODI"] ?>">
<input type="hidden" name="xRADI_NUME_RADI" value="<?php echo $row["RADI_NUME_RADI"] ?>">
<input type="hidden" name="xUSUA_CODI_DEST" value="<?php echo $row["USUA_CODI_DEST"] ?>">
<input type="hidden" name="xUSUA_DOC" value="<?php echo $row["USUA_DOC"] ?>">
<input type="hidden" name="xUSUA_DOC_OLD" value="<?php echo $row["USUA_DOC_OLD"] ?>">
<input type="hidden" name="xSGD_TTR_CODIGO" value="<?php echo $row["SGD_TTR_CODIGO"] ?>">
<input type="hidden" name="xHIST_USUA_AUTOR" value="<?php echo $row["HIST_USUA_AUTOR"] ?>">
<input type="hidden" name="xHIST_DOC_DEST" value="<?php echo $row["HIST_DOC_DEST"] ?>">
<input type="hidden" name="xDEPE_CODI_DEST" value="<?php echo $row["DEPE_CODI_DEST"] ?>">
<input type="hidden" name="xUSUA_IP" value="<?php echo $row["USUA_IP"] ?>">
<?php showrow($row, $recid) ?>
<p><input type="submit" name="action" value="<?php echo "Confirm" ?>"></p>
</form>
<?php
  ocifreestatement($res);
} ?>

<?php function convertToUtf8($str){
  global $PAGE_ENCODING;
  return convert($PAGE_ENCODING, "UTF-8", $str);
}

function convertFromUtf8($str)
{
  global $PAGE_ENCODING;
  return convert("UTF-8", $PAGE_ENCODING, $str);
}

function convert($from, $to, $str){
  if ($from != "" && $to != "" && $from != $to) {
    if (function_exists("iconv")) {
      return iconv($from, $to, $str);
    } elseif (function_exists("mb_convert_encoding")) {
      return mb_convert_encoding($str, $to, $from);
    } else {
      return $str;
    }
  } else {
    return $str;
  }
}
function connect()
{
  $conn = ocilogon("orfeo", "orfeo2008", "172.25.2.15:1521/orfeo");
  return $conn;
}

function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  //$sql = "SELECT * FROM (SELECT t1.DEPE_CODI, lp0.DEPE_CODI AS \"lp_DEPE_CODI\", t1.HIST_FECH, t1.USUA_CODI, t1.RADI_NUME_RADI, lp3.RADI_NUME_RADI AS \"lp_RADI_NUME_RADI\", t1.HIST_OBSE, t1.USUA_CODI_DEST, t1.USUA_DOC, t1.USUA_DOC_OLD, t1.SGD_TTR_CODIGO, t1.HIST_USUA_AUTOR, t1.HIST_DOC_DEST, t1.DEPE_CODI_DEST, t1.USUA_IP FROM ORFEO.HIST_EVENTOS AS t1 LEFT OUTER JOIN ORFEO.DEPENDENCIA AS lp0 ON (t1.DEPE_CODI = lp0.DEPE_CODI) LEFT OUTER JOIN ORFEO.RADICADO AS lp3 ON (t1.RADI_NUME_RADI = lp3.RADI_NUME_RADI)) subq";
// $sql = "SELECT * FROM hist_eventos";
 $sql = "SELECT * FROM hist_eventos,dependencia where hist_eventos.depe_codi=dependencia.depe_codi";


  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (\"lp_DEPE_CODI\" like '" .$filterstr ."') or (HIST_FECH like '" .$filterstr ."') or (USUA_CODI like '" .$filterstr ."') or (\"lp_RADI_NUME_RADI\" like '" .$filterstr ."') or (HIST_OBSE like '" .$filterstr ."') or (USUA_CODI_DEST like '" .$filterstr ."') or (USUA_DOC like '" .$filterstr ."') or (USUA_DOC_OLD like '" .$filterstr ."') or (SGD_TTR_CODIGO like '" .$filterstr ."') or (HIST_USUA_AUTOR like '" .$filterstr ."') or (HIST_DOC_DEST like '" .$filterstr ."') or (DEPE_CODI_DEST like '" .$filterstr ."') or (USUA_IP like '" .$filterstr ."')";
  }
  //if (isset($order) && $order!='') $sql .= " order by \"" .sqlstr($order) ."\"";
  //if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype);
//  echo $sql."aca";
 //echo "aca toy1".$sql;
  $res = ociquery($sql);

  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
 // $sql = "SELECT COUNT(*) FROM (SELECT t1.DEPE_CODI, lp0.DEPE_CODI AS \"lp_DEPE_CODI\", t1.HIST_FECH, t1.USUA_CODI, t1.RADI_NUME_RADI, lp3.RADI_NUME_RADI AS \"lp_RADI_NUME_RADI\", t1.HIST_OBSE, t1.USUA_CODI_DEST, t1.USUA_DOC, t1.USUA_DOC_OLD, t1.SGD_TTR_CODIGO, t1.HIST_USUA_AUTOR, t1.HIST_DOC_DEST, t1.DEPE_CODI_DEST, t1.USUA_IP FROM ORFEO.HIST_EVENTOS AS t1 LEFT OUTER JOIN ORFEO.DEPENDENCIA AS lp0 ON (t1.DEPE_CODI = lp0.DEPE_CODI) LEFT OUTER JOIN ORFEO.RADICADO AS lp3 ON (t1.RADI_NUME_RADI = lp3.RADI_NUME_RADI)) subq";
                                 $sql = "SELECT * FROM hist_eventos";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (\"lp_DEPE_CODI\" like '" .$filterstr ."') or (HIST_FECH like '" .$filterstr ."') or (USUA_CODI like '" .$filterstr ."') or (\"lp_RADI_NUME_RADI\" like '" .$filterstr ."') or (HIST_OBSE like '" .$filterstr ."') or (USUA_CODI_DEST like '" .$filterstr ."') or (USUA_DOC like '" .$filterstr ."') or (USUA_DOC_OLD like '" .$filterstr ."') or (SGD_TTR_CODIGO like '" .$filterstr ."') or (HIST_USUA_AUTOR like '" .$filterstr ."') or (HIST_DOC_DEST like '" .$filterstr ."') or (DEPE_CODI_DEST like '" .$filterstr ."') or (USUA_IP like '" .$filterstr ."')";
  }
  $res = ociquery($sql);
  $row = ocifetchassoc($res);
  reset($row);
  return current($row);
}

function sql_insert()
{
  global $conn;
  global $_POST;

  $sql = "insert into ORFEO.HIST_EVENTOS (DEPE_CODI, HIST_FECH, USUA_CODI, RADI_NUME_RADI, HIST_OBSE, USUA_CODI_DEST, USUA_DOC, USUA_DOC_OLD, SGD_TTR_CODIGO, HIST_USUA_AUTOR, HIST_DOC_DEST, DEPE_CODI_DEST, USUA_IP) values (" .sqlvalue(@$_POST["DEPE_CODI"], false).", " .sqlvalue(@$_POST["HIST_FECH"], true).", " .sqlvalue(@$_POST["USUA_CODI"], false).", " .sqlvalue(@$_POST["RADI_NUME_RADI"], false).", " .sqlvalue(@$_POST["HIST_OBSE"], true).", " .sqlvalue(@$_POST["USUA_CODI_DEST"], false).", " .sqlvalue(@$_POST["USUA_DOC"], true).", " .sqlvalue(@$_POST["USUA_DOC_OLD"], true).", " .sqlvalue(@$_POST["SGD_TTR_CODIGO"], false).", " .sqlvalue(@$_POST["HIST_USUA_AUTOR"], true).", " .sqlvalue(@$_POST["HIST_DOC_DEST"], true).", " .sqlvalue(@$_POST["DEPE_CODI_DEST"], false).", " .sqlvalue(@$_POST["USUA_IP"], true).")";
  ociquery($sql);
}

function sql_update()
{
  global $conn;
  global $_POST;

  $sql = "update ORFEO.HIST_EVENTOS set DEPE_CODI=" .sqlvalue(@$_POST["DEPE_CODI"], false).", HIST_FECH=" .sqlvalue(@$_POST["HIST_FECH"], true).", USUA_CODI=" .sqlvalue(@$_POST["USUA_CODI"], false).", RADI_NUME_RADI=" .sqlvalue(@$_POST["RADI_NUME_RADI"], false).", HIST_OBSE=" .sqlvalue(@$_POST["HIST_OBSE"], true).", USUA_CODI_DEST=" .sqlvalue(@$_POST["USUA_CODI_DEST"], false).", USUA_DOC=" .sqlvalue(@$_POST["USUA_DOC"], true).", USUA_DOC_OLD=" .sqlvalue(@$_POST["USUA_DOC_OLD"], true).", SGD_TTR_CODIGO=" .sqlvalue(@$_POST["SGD_TTR_CODIGO"], false).", HIST_USUA_AUTOR=" .sqlvalue(@$_POST["HIST_USUA_AUTOR"], true).", HIST_DOC_DEST=" .sqlvalue(@$_POST["HIST_DOC_DEST"], true).", DEPE_CODI_DEST=" .sqlvalue(@$_POST["DEPE_CODI_DEST"], false).", USUA_IP=" .sqlvalue(@$_POST["USUA_IP"], true) ." where " .primarykeycondition();
  ociquery($sql);
}

function sql_delete()
{
  global $conn;

  $sql = "delete from ORFEO.HIST_EVENTOS where " .primarykeycondition();
  ociquery($sql);
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(DEPE_CODI";
  if (@$_POST["xDEPE_CODI"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xDEPE_CODI"], false);
  };
  $pk .= ") and ";
  $pk .= "(HIST_FECH";
  if (@$_POST["xHIST_FECH"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xHIST_FECH"], true);
  };
  $pk .= ") and ";
  $pk .= "(USUA_CODI";
  if (@$_POST["xUSUA_CODI"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xUSUA_CODI"], false);
  };
  $pk .= ") and ";
  $pk .= "(RADI_NUME_RADI";
  if (@$_POST["xRADI_NUME_RADI"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xRADI_NUME_RADI"], false);
  };
  $pk .= ") and ";
  $pk .= "(USUA_CODI_DEST";
  if (@$_POST["xUSUA_CODI_DEST"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xUSUA_CODI_DEST"], false);
  };
  $pk .= ") and ";
  $pk .= "(USUA_DOC";
  if (@$_POST["xUSUA_DOC"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xUSUA_DOC"], true);
  };
  $pk .= ") and ";
  $pk .= "(USUA_DOC_OLD";
  if (@$_POST["xUSUA_DOC_OLD"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xUSUA_DOC_OLD"], true);
  };
  $pk .= ") and ";
  $pk .= "(SGD_TTR_CODIGO";
  if (@$_POST["xSGD_TTR_CODIGO"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xSGD_TTR_CODIGO"], false);
  };
  $pk .= ") and ";
  $pk .= "(HIST_USUA_AUTOR";
  if (@$_POST["xHIST_USUA_AUTOR"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xHIST_USUA_AUTOR"], true);
  };
  $pk .= ") and ";
  $pk .= "(HIST_DOC_DEST";
  if (@$_POST["xHIST_DOC_DEST"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xHIST_DOC_DEST"], true);
  };
  $pk .= ") and ";
  $pk .= "(DEPE_CODI_DEST";
  if (@$_POST["xDEPE_CODI_DEST"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xDEPE_CODI_DEST"], false);
  };
  $pk .= ") and ";
  $pk .= "(USUA_IP";
  if (@$_POST["xUSUA_IP"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xUSUA_IP"], true);
  };
  $pk .= ")";
  return $pk;
}


function ociquery($sql)
{
  global $conn;

  $res = ociparse($conn, $sql);
  ociexecute($res, OCI_DEFAULT) or die(ocierror());
  ocicommit($conn);
  return $res;
}

function ocifetchassoc($res)
{
  $ret_array = array();
  ocifetchinto($res, $ret_array, OCI_ASSOC);
  return $ret_array;
} ?>
